module.exports = function($scope, $modal, $modalInstance, $resource, activity, info, toaster){
    
    $scope.formData = {};
    $scope.activity = activity;
    
    //加载已经分配的部门列表
    $scope.loadPart = function(){
        $scope.remarklist = [];
        $scope.allocatedlist = [];
        var para = {
            'level': '1',
            'activity_id': activity.activity_id,
            'tour_date': activity.tour_date
        }
        $resource('/api/ac/ggc/voluntourismAccountService/getAllocatedList', {}, {}).save(para, function (res) {
            if (res.errcode === 0){
                $scope.allocated_num = res.data.allocated_num;
                angular.forEach(res.data.list, function(item){
                    if(item.pwxids){
                        item.modify_num = item.num;
                        $scope.allocatedlist.push(item);
                    } else {
                        $scope.remarklist.push(item);
                    }
                });
            } else {
                toaster.error({title: '', body: '查询分配信息失败, 请刷新'});
            }
        });
    }
    $scope.loadPart();

    $scope.add = function(){
        if(!$scope.formData.company){
            toaster.warning({title: '', body: '请选择部门'});
            return;
        }
        if(!$scope.formData.num || $scope.formData.num <= 0){
            toaster.warning({title: '', body: '请填写分配名额'});
            return;
        }
        if(activity.tour_stock - $scope.allocated_num < $scope.formData.num){
            toaster.warning({title: '', body: '剩余名额不足'});
            return;
        }
        var para = {
            'level': '1',
            'nickname': $scope.formData.company.nickname,
            'activity_id': activity.activity_id,
            'tour_date': activity.tour_date,
            'tour_stock': $scope.formData.num,
            'wxid': $scope.formData.company.wxuid
        }
        $resource('/api/ac/ggc/voluntourismAccountService/setAllocat', {}, {}).save(para, function (res){
            if (res.errcode === 0){
                toaster.success({title: '', body: '添加成功'});
                $scope.loadPart();
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    $scope.save = function(obj) {
        if(obj.modify_num <= 0){
            toaster.warning({title: '', body: '修改名额错误'});
            return;
        }
        var para = {
            'level': '1',
            'activity_id': activity.activity_id,
            'tour_date': activity.tour_date,
            'tour_stock': obj.modify_num - obj.num,
            'wxid': obj.wxuid
        }
        $resource('/api/ac/ggc/voluntourismAccountService/setAllocat', {}, {}).save(para, function(res){
            if (res.errcode === 0){
                toaster.success({title: '', body: '修改成功'});
                obj.update = false;
                obj.num = obj.modify_num;
                obj.surplus_stock += para.tour_stock;
                $scope.allocated_num += para.tour_stock;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    $scope.delete = function(obj){
        if(confirm('确认删除已经分配的名额吗？')){
            $resource('/api/as/ggc/volaccount/deleteAccountWx', {}, {}).save(obj, function (res) {
                if (res.errcode === 0){
                    toaster.success({title: '', body: '删除成功'});
                    $scope.loadPart();
                } else {
                    toaster.error({title: '', body: '删除失败, 请重试'});
                }
            });
        }
    }

    $scope.close = function () {
        $modalInstance.dismiss('cancel');
    }


};